<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="类别编号" prop="cid">
        <el-input
          v-model="queryParams.cid"
          placeholder="请输入类别编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="gname">
        <el-input
          v-model="queryParams.gname"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品序列号" prop="gsn">
        <el-input
          v-model="queryParams.gsn"
          placeholder="请输入商品序列号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:goods:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="goodsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品编号" align="center" prop="gid" fixed />
      <el-table-column label="类别编号" align="center" prop="cid" />
      <el-table-column label="商品名称" align="center" prop="gname" />
      <el-table-column label="商品图片" align="center" prop="gimg" width="100">
        <template slot-scope="scope">
          <!-- <image-preview :src="scope.row.gimg" :width="50" :height="50"/> v-if="scope.row.gimg"  v-for="imgs in scope.row,gimg.split(',')" :key="imgs"-->
          <img :src="$baseUrl + scope.row.gimg" :width="50" :height="50" />
        </template>
        <!-- <template v-else>
          <span>无图片</span>
        </template> -->
      </el-table-column>
      <el-table-column label="商品序列号" align="center" prop="gsn" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="销售价" align="center" prop="saleprice" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="详细标题" align="center" prop="detailtitle" />
      <el-table-column label="库存" align="center" prop="stock" />
      <el-table-column label="低库存" align="center" prop="lowstock" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="重量" align="center" prop="weight" />
      <el-table-column label="发布状态" align="center" prop="publishstate">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.publishstate == '0'" type="danger">否</el-tag>
          <el-tag v-else>是</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="积分" align="center" prop="giftpoint" />
      <el-table-column label="验证状态" align="center" prop="verifystate" />
      <el-table-column
        label="送达时间"
        align="center"
        prop="sendtime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendtime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已售数量" align="center" prop="salednum" />
      <el-table-column label="销售总计" align="center" prop="saletotal" />
      <el-table-column label="注意" align="center" prop="note" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:goods:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:goods:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      small
      v-show="total > 0"
      :total="total"
      :current-page.sync="queryParams.pageNum"
      :page-size.sync="queryParams.pageSize"
      background
      layout="prev,pager,next"
      @current-change="getList"
    />
    <!-- 添加或修改goods对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类别编号" prop="cid">
          <el-input v-model="form.cid" placeholder="请输入类别编号" />
        </el-form-item>
        <el-form-item label="商品名称" prop="gname">
          <el-input v-model="form.gname" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品图片" prop="gimg">
          <el-input type="hidden" v-model="form.gimg" />
          <el-upload
            :action="$baseUrl + '/upload'"
            ref="upload"
            class="upload-demo"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            :on-success="handleAvatarSuccess"
            :auto-upload="false"
          >
            <el-button slot="trigger" size="small" type="primary"
              >选取文件</el-button
            >
            <el-button
              style="margin-left: 10px"
              size="small"
              type="sucess"
              @click="submitUpload"
              >上传到服务器</el-button
            >
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" />
          </el-dialog>
        </el-form-item>
        <el-form-item label="商品序列号" prop="gsn">
          <el-input v-model="form.gsn" placeholder="请输入商品序列号" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="销售价" prop="saleprice">
          <el-input v-model="form.saleprice" placeholder="请输入销售价" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            v-model="form.description"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="详细标题" prop="detailtitle">
          <el-input v-model="form.detailtitle" placeholder="请输入详细标题" />
        </el-form-item>
        <el-form-item label="库存" prop="stock">
          <el-input v-model="form.stock" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="低库存" prop="lowstock">
          <el-input v-model="form.lowstock" placeholder="请输入低库存" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="重量" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入重量" />
        </el-form-item>
        <el-form-item label="发布状态" prop="publishstate">
          <template>
            <el-radio v-model="form.publishstate" label="1">是</el-radio>
            <el-radio v-model="form.publishstate" label="0">否</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="积分" prop="giftpoint">
          <el-input v-model="form.giftpoint" placeholder="请输入积分" />
        </el-form-item>
        <el-form-item label="验证状态" prop="verifystate">
          <el-input v-model="form.verifystate" placeholder="请输入验证状态" />
        </el-form-item>
        <el-form-item label="送达时间" prop="sendtime">
          <template>
            <el-date-picker
              v-model="form.sendtime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择送达时间"
            >
            </el-date-picker>
          </template>
        </el-form-item>
        <el-form-item label="已售数量" prop="salednum">
          <el-input v-model="form.salednum" placeholder="请输入已售数量" />
        </el-form-item>
        <el-form-item label="销售总计" prop="saletotal">
          <el-input v-model="form.saletotal" placeholder="请输入销售总计" />
        </el-form-item>
        <el-form-item label="注意" prop="note">
          <el-input v-model="form.note" placeholder="请输入注意" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGoods,
  getGoods,
  delGoods,
  addGoods,
  updateGoods,
} from "@/api/system/goods";
import { parseTime } from "@/utils";
export default {
  name: "Goods",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // goods表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cid: null,
        gname: null,
        gimg: null,
        gsn: null,
        price: null,
        saleprice: null,
        description: null,
        detailtitle: null,
        stock: null,
        lowstock: null,
        unit: null,
        weight: null,
        publishstate: null,
        giftpoint: null,
        verifystate: null,
        sendtime: null,
        salednum: null,
        saletotal: null,
        note: null,
        sort: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //上传头像
      imageUrl: "",
      dialogImageUrl: "",
      dialogVisible: false,
      disabled:false,
      fileList:[],
      baseUrl: this.$baseUrl,
      
    };
  },
  created() {
    this.getList();
  },
  methods: {
    parseTime,
    /** 查询goods列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then((response) => {
        this.goodsList = response.data.rows;
        this.total = response.data.totalsize;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        gid: null,
        cid: null,
        gname: null,
        gimg: null,
        gsn: null,
        price: null,
        saleprice: null,
        description: null,
        detailtitle: null,
        stock: null,
        lowstock: null,
        unit: null,
        weight: null,
        publishstate: null,
        giftpoint: null,
        verifystate: null,
        sendtime: null,
        salednum: null,
        saletotal: null,
        note: null,
        sort: null,
      };
      this.$nextTick(() => {
        this.$refs["form"].resetFields();
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.gid);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加goods";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const gid = row.gid || this.ids;
      getGoods(gid).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改goods";
        this.fileList = [];
        if(this.form.gimg!=null){
          const names = this.form.gimg.split(",");
          names.forEach(item=>{
            this.fileList.push({name:item});
          })
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.gid != null) {
            console.log(this.fileList.length+" ");
            if(this.fileList.length>0){
              this.form.gimg = this.fileList.map(item=>item.name).join(",");
            }
            updateGoods(this.form).then((response) => {
              this.$message({ type: "success", message: "修改成功" });
              // this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log(this.fileList.length+" ");
            if(this.fileList.length>0){
              this.form.gimg = this.fileList.join(",");
            }
            addGoods(this.form).then((response) => {
              this.$message({ type: "success", message: "新增成功" });
              // this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const gids = row.gid || this.ids;
      this.$confirm('是否确认删除goods编号为"' + gids + '"的数据项？', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delGoods(gids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
     // 上传头像
    handleAvatarSuccess(res, file) {
      //上传成功之后的回调函数
      console.log(res.data + " ");
      this.fileList.push(res.data);
      console.log(this.fileList);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    //修改头像
    handleAvatarSuccess1(res, file) {
      this.form.gimg = res.data;
    },
    // 照片墙
    handleRemove(file, fileList) {
      this.fileList = [];
      fileList.forEach(item => {
        this.fileList.push(item.name);
      });
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    submitUpload() {
        this.$refs.upload.submit();
      },
     handlePreview(file) {
        console.log(file);
      },
  },
};
</script>
<style scoped>
/* 头像上传 */
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
